package com.cos.hospital.init;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cos.hospital.model.retrofit.HostWrapper;
import com.cos.hospital.model.retrofit.RequestServes;
import com.cos.hospital.tool.UDialog;

import butterknife.ButterKnife;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @Description: <p>
 * @Author: Kosmos
 * @Date: 2018年08月02日 15:01
 * @Email: KosmoSakura@foxmail.com
 */
public abstract class BaseFragment extends Fragment {
    protected Context context;
    protected CompositeDisposable compositeDisposable;
    private RequestServes requestServes;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        requestServes = HostWrapper.with().create(RequestServes.class);
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.w("info", "onStart " + this.toString());
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.w("info", "onResume " + this.toString());
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(layout(), container, false);
        ButterKnife.bind(this, contentView);
        init();
        logic();
        return contentView;
    }

    protected RequestServes getServes() {
        return requestServes;
    }

    protected void addEvent(Disposable disposable) {
        compositeDisposable.add(disposable);
    }

    /**
     * @return 返回布局
     */
    protected abstract int layout();

    /**
     * 初始化基础信息
     */
    protected abstract void init();

    /**
     * 填充逻辑部分 include
     */
    protected abstract void logic();

    /**
     * @param notice 显示页面错误提示
     */
    protected void showNotice(String notice) {
        UDialog.getInstance(context, false, false)
                .showNoticeWithOnebtn(notice);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (compositeDisposable != null) {
            compositeDisposable.clear();
        }
    }
}
